package com.rongshe.web.controller.hotel;

import java.io.BufferedInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.URL;
import java.nio.file.Paths;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.rongshe.common.config.RongsheConfig;
import com.rongshe.common.constant.Constants;
import com.rongshe.common.core.domain.entity.SysUser;
import com.rongshe.common.exception.file.FileNameLengthLimitExceededException;
import com.rongshe.common.exception.file.FileSizeLimitExceededException;
import com.rongshe.common.utils.DateUtils;
import com.rongshe.common.utils.StringUtils;
import com.rongshe.common.utils.file.FileUploadUtils;
import com.rongshe.common.utils.file.FileUtils;
import com.rongshe.common.utils.file.ImageUtils;
import com.rongshe.common.utils.uuid.Seq;
import com.rongshe.framework.config.ServerConfig;
import com.rongshe.system.service.ISysUserService;
import org.apache.commons.io.FilenameUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.rongshe.common.annotation.Log;
import com.rongshe.common.core.controller.BaseController;
import com.rongshe.common.core.domain.AjaxResult;
import com.rongshe.common.enums.BusinessType;
import com.rongshe.hotel.domain.RsUser;
import com.rongshe.hotel.service.IRsUserService;
import com.rongshe.common.utils.poi.ExcelUtil;
import com.rongshe.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 用户Controller
 *
 * @author rongshe
 * @date 2025-02-18
 */
@RestController
@RequestMapping("/hotel/user")
public class RsUserController extends BaseController {
    @Autowired
    private IRsUserService rsUserService;
    @Autowired
    private ISysUserService sysUserService;
    @Autowired
    private ServerConfig serverConfig;
    /**
     * 查询用户列表
     */
    @PreAuthorize("@ss.hasPermi('hotel:user:list')")
    @GetMapping("/list")
    public TableDataInfo list(RsUser rsUser) {
        startPage();
        List<RsUser> list = rsUserService.selectRsUserList(rsUser);
        return getDataTable(list);
    }

    /**
     * 导出用户列表
     */
    @PreAuthorize("@ss.hasPermi('hotel:user:export')")
    @Log(title = "用户", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, RsUser rsUser) {
        List<RsUser> list = rsUserService.selectRsUserList(rsUser);
        ExcelUtil<RsUser> util = new ExcelUtil<RsUser>(RsUser.class);
        util.exportExcel(response, list, "用户数据");
    }

    /**
     * 获取用户详细信息
     */
    @PreAuthorize("@ss.hasPermi('hotel:user:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(rsUserService.selectRsUserById(id));
    }

    /**
     * 新增用户
     */
    @PreAuthorize("@ss.hasPermi('hotel:user:add')")
    @Log(title = "用户", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody RsUser rsUser) {
        return toAjax(rsUserService.insertRsUser(rsUser));
    }

    /**
     * 修改用户
     */
    @Log(title = "用户", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody RsUser rsUser) {
        int result = rsUserService.updateRsUser(rsUser);
        if (result > 0) {
            if (StringUtils.isNotEmpty(rsUser.getStatus())) {
                SysUser sysUser = new SysUser();
                sysUser.setUserId(rsUser.getUserId());
                sysUser.setStatus(rsUser.getStatus());
                sysUserService.updateUser(sysUser);
            }
        }
        return toAjax(result);
    }

    /**
     * 删除用户
     */
    @PreAuthorize("@ss.hasPermi('hotel:user:remove')")
    @Log(title = "用户", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        Long[] userIds = new Long[ids.length];
        for (int i = 0; i < ids.length; i++) {
            userIds[i] = rsUserService.selectRsUserById(ids[i]).getUserId();
        }
        sysUserService.deleteUserByIds(userIds);
        int result = rsUserService.deleteRsUserByIds(ids);
        return toAjax(result);
    }

    @PostMapping("/uploadAvatar")
    public AjaxResult uploadAvatar(@RequestBody RsUser rsUser) throws Exception
    {
        try
        {
            // 上传文件路径
            String filePath = RongsheConfig.getUploadPath();
            String imageUrl = "https://thirdwx.qlogo.cn/mmopen/vi_32/myU16RLuxDeic24ibFTSqvXVXfop8iad10jsk7B5vvEqotsbpVQWBDOHRaMPPtBnyJpV36XHq92o5D2lXwEK1rYZlq6RCSicicxDR0gY0udiaeMFg/132";
            byte[] bytes = ImageUtils.readFile(imageUrl);
            String fileUrl = FileUtils.writeBytes(bytes, filePath);
            AjaxResult ajax = AjaxResult.success();
            ajax.put("imgUrl", fileUrl);
            return ajax;
        }
        catch (Exception e)
        {
            return AjaxResult.error(e.getMessage());
        }
    }

}
